﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Open_MUD.Mudlib.lib.Interactive.Animales;
using System.Xml;
using System.IO;
using Open_MUD.Kernel;
using Open_MUD.Mudlib.lib.Interactive;
using Open_MUD.Mudlib.lib.Loc;
using System.Globalization;
using Open_MUD.Kernel.Server.Services;

namespace Open_MUD.Mudlib.lib
{
    class Jugador:Humanoide
    {
	
        bool loaded = false;
		
		#region Constantes
		
		public enum Raza {stark,targaryen,tully,arryn,baratheon,greyjoy,lannister,martell,tyrell,errante};
		public string [] strRaza = new string [10]{"stark","targaryen","tully","arryn","baratheon","greyjoy","lannister","martell","tyrell","errante"};
		
		public enum Subraza{umber,reed,bolton,frey,blackwood,whent,royce,baelish,corbay,selmy,tarth,dondarrion,harlaw,sparr,saltcliffe,payne,clegane,swyft,dayne,yronwood,manwoody,oakheart,hightower,redwyne};
		public string [] strSubraza = new string [24]{"umber","reed","bolton","frey","blackwood","whent","royce","baelish","corbay","selmy","tarth","dondarrion","harlaw","sparr","saltcliffe","payne","clegane","swyft","dayne","yronwood","manwoody","oakheart","hightower","redwyne"};
        
		
		//public final string [] RAZAS = new string[10] {RAZA_STARK,RAZA_TARGARYEN,RAZA_TULLY,RAZA_ARRYN, RAZA_BARATHEON,RAZA_GREYJOY,RAZA_LANNISTER, RAZA_MARTELL,RAZA_TYRELL,RAZA_ERRANTE};
		
		//Stark 
		public const string SUBRAZA_UMBER = "umber";
		public const string SUBRAZA_REED = "reed";
		public const string SUBRAZA_BOLTON = "bolton";
		
		//Targaryen
		/*
		public const string SUBRAZA_
		public const string SUBRAZA_
		public const string SUBRAZA_ */
		
		//Tully
		public const string SUBRAZA_FREY = "frey";
		public const string SUBRAZA_BLACKWOOD = "blackwood";
		public const string SUBRAZA_WHENT = "whent";
		
		//Arryn
		public const string SUBRAZA_ROYCE = "royce";
		public const string SUBRAZA_BAELISH = "baelish";
		public const string SUBRAZA_CORBRAY = "corbray";
		
		//Baratheon
		public const string SUBRAZA_SELMY = "selmy";
		public const string SUBRAZA_TARTH = "tarth";
		public const string SUBRAZA_DONDARRION = "dondarrion";
		
		//Greyjoy: WHAT IS DEAD MAY NEVER DIE!!!!!
		public const string SUBRAZA_HARLAW = "harlaw";
		public const string SUBRAZA_SPARR = "sparr";
		public const string SUBRAZA_SALTCLIFFE = "saltcliffe";
		
		//Lannister
		public const string SUBRAZA_PAYNE = "payne";
		public const string SUBRAZA_CLEGANE = "clegane";
		public const string SUBRAZA_SWYFT = "swyft";
		
		//Martell
		public const string SUBRAZA_DAYNE = "dayne";
		public const string SUBRAZA_YRONWOOD = "yronwood";
		public const string SUBRAZA_MANWOODY = "manwoody";
		
		//Tyrell
		public const string SUBRAZA_OAKHEART = "oakheart";
		public const string SUBRAZA_HIGHTOWER = "hightower";
		public const string SUBRAZA_REDWINE = "redwine";
		
		#endregion
       
        

        public void loadPJ(String pj)
        {
            if (!loaded)
            {              
                pj = pj.ToLower().Trim();
                nombre = pj;
                XmlDocument xmldata = AuthenticationService.getInstance().getPjXml(pj);                                                
                string xmllong = xmldata.SelectSingleNode("player/long").InnerText;
                this.Long = xmllong;
                nivel = xmldata.SelectSingleNode("player/level").InnerText;
                raza = xmldata.SelectSingleNode("player/race").InnerText;
                subraza = xmldata.SelectSingleNode("player/subrace").InnerText;
                edad = xmldata.SelectSingleNode("player/edad").InnerText;
                ciudadania = xmldata.SelectSingleNode("player/ciudadania").InnerText;
                localizacion = xmldata.SelectSingleNode("player/localizacion").InnerText;
                altura = xmldata.SelectSingleNode("player/altura").InnerText;
                ojos = xmldata.SelectSingleNode("player/ojos").InnerText;
                pelo = xmldata.SelectSingleNode("player/pelo").InnerText;
                complexion = xmldata.SelectSingleNode("player/complexion").InnerText;
                clase = xmldata.SelectSingleNode("player/clase").InnerText;
                    
                caracteristicas = new Caracteristicas(
                   xmldata.SelectSingleNode("player/fuerza").InnerText,
                   xmldata.SelectSingleNode("player/constitucion").InnerText,
                   xmldata.SelectSingleNode("player/destreza").InnerText,
                   xmldata.SelectSingleNode("player/inteligencia").InnerText,
                   xmldata.SelectSingleNode("player/carisma").InnerText,
                   xmldata.SelectSingleNode("player/sabiduria").InnerText,
                   xmldata.SelectSingleNode("player/fortaleza").InnerText,
                   xmldata.SelectSingleNode("player/reflejos").InnerText,
                   xmldata.SelectSingleNode("player/voluntad").InnerText                       
                );

                if (xmllong.Equals(""))
                    this.Long = this.toString();

                this.PF = Int32.Parse(xmldata.SelectSingleNode("player/PF").InnerText);
                this.Short = colorear(pj);
                string id_mapa = "westeros";
                int pjx = 0 ;
                int pjy = 0 ;
                force_move(MapHandler.getInstance().getMapa(id_mapa).GetRoom(pjx,pjy));                
                loaded = true;
            }
        }

        public string toString()
        {
            return nombre + " es un " + raza.ToLowerInvariant() + " de nivel " + nivel + ", tiene " + edad + " años y viene de " + ciudadania;
        }

        private Raza strToRaza(String raza)
        {
            return Raza.stark;
        }

        private string colorear(string pj)
        {
			// Capitalizar nombre
            pj = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(pj);
            
            switch (strToRaza(raza))
            {
                case Raza.stark: return "%^reset%^BOLD%^BLACK%^" + pj + "%^reset%^"; break;
                case Raza.lannister: return "%^reset%^RED%^" + pj + "%^reset%^"; break;
                case Raza.tully: return "%^reset%^CYAN%^" + pj + "%^reset%^"; break;
                case Raza.tyrell: return "%^reset%^BOLD%^GREEN%^" + pj + "%^reset%^"; break;
                case Raza.targaryen: return "%^reset%^BOLD%^RED%^" + pj + "%^reset%^"; break;
                case Raza.greyjoy: return "%^reset%^BLUE%^" + pj + "%^reset%^"; break;
                case Raza.baratheon: return "%^reset%^YELLOW%^" + pj + "%^reset%^"; break;
                case Raza.arryn: return "%^reset%^BOLD%^BLUE%^" + pj + "%^reset%^"; break;
                case Raza.martell: return "%^reset%^ORANGE%^" + pj + "%^reset%^"; break;
                case Raza.errante: return "%^reset%^MAGENTA%^" + pj + "%^reset%^"; break;
            }
            return pj;
        }

    }
}
